# Replication Code for Figures
# James M. Curry and Leah Rosenstiel
# Last Updated 12/3/2025

rm(list=ls())
library(tidyverse)
source("functions.R")

# Read in the data
grants <- read_csv("grants.csv")
grants <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 1, 0),
         treated = on_commit * unorthodox, #treated units are unorthdox law and on committee
         control = on_commit * (1 - unorthodox), #control units are regular law and on committee 
         lag_control = 1 - on_commit,
         state_program = paste(state_abbr, program),
         year_cong = paste(year, congress),
         log_grant = log(grant+1),
         program_year = paste(program,year)) 


#### Count of Reauthorizations (Figure 1) ####
grants %>%
  mutate(bill_type = ifelse(s_nocommreport + no_conference == 2, "No Conference or Senate Report", 
                            ifelse(s_nocommreport==1, "No Senate Report",
                                   ifelse(no_conference==1,"No Conference","Regular Order"))),
         bill_type=factor(bill_type,
                          levels = c("No Conference or Senate Report","No Senate Report",
                                     "No Conference","Regular Order"),
                          ordered = T))%>%
  select(congress,unorthodox,bill_type,program) %>%
  unique() %>%
  group_by(congress,bill_type) %>%
  summarise(count_programs=n())%>%
  ggplot(aes(x=congress,fill=bill_type,
             y=count_programs)) +
  #geom_col(position = position_dodge(preserve = "single")) +
  geom_col()+
  scale_x_continuous(name="Congress",breaks=seq(95,115,5)) +
  ylab("Count of Programs Reauthorized")+
  scale_fill_brewer(palette = "Reds",name=element_blank()) +
  theme_minimal() +
  theme(legend.position = "bottom")

#### ED Programs (Figure A1) ####
data_for_fig <- grants %>%
  mutate(unorthodox = ifelse(s_nocommreport + no_conference > 0, 
                             "Unorthodox", "Regular Order")) %>%
  filter(!is.na(name) & (year == year_auth_starts)) %>%
  select(program, congress, unorthodox, name, agency) %>%
  unique
filter(data_for_fig,agency=="ED") %>%
  ggplot(aes(x=name,y=congress)) +
  geom_point(aes(color=unorthodox))+
  scale_y_continuous(name="Congress",breaks = seq(from=94,to=114,by=2)) +
  coord_flip() +
  theme_minimal() +
  theme(axis.title.y=element_blank(),
        legend.position = "bottom",
        legend.title = element_blank())

#### Housing and Energy Programs (Figure A2) ####
filter(data_for_fig,agency%in%c("DOE","HUD")) %>%
  ggplot(aes(x=name,y=congress)) +
  geom_point(aes(color=unorthodox))+
  scale_y_continuous(name="Congress",breaks = seq(from=94,to=114,by=2)) +
  coord_flip() +
  theme_minimal() +
  theme(axis.title.y=element_blank(),
        legend.position = "bottom",
        legend.title = element_blank())

#### Agriculture Programs (Figure A3) ####
filter(data_for_fig,agency%in%c("USDA")) %>%
  ggplot(aes(x=name,y=congress)) +
  geom_point(aes(color=unorthodox))+
  scale_y_continuous(name="Congress",breaks = seq(from=94,to=114,by=2)) +
  coord_flip() +
  theme_minimal() +
  theme(axis.title.y=element_blank(),
        legend.position = "bottom",
        legend.title = element_blank())

#### HHS Programs (Figure A4) ####
filter(data_for_fig,agency=="HHS") %>%
  ggplot(aes(x=name,y=congress)) +
  geom_point(aes(color=unorthodox))+
  scale_y_continuous(name="Congress",breaks = seq(from=94,to=114,by=2)) +
  coord_flip() +
  theme_minimal() +
  theme(axis.title.y=element_blank(),
        legend.position = "bottom",
        legend.title = element_blank())


#### Transportation Programs (Figure A5) ####
filter(data_for_fig,
       agency=="DOT") %>%
  ggplot(aes(x=name,y=congress)) +
  geom_point(aes(color=unorthodox))+
  scale_y_continuous(name="Congress",breaks=seq(100,116,2)) +
  coord_flip() +
  theme_minimal() +
  theme(axis.title.y=element_blank(),
        legend.position = "bottom",
        legend.title = element_blank())

##### Labor Programs (Figure A6) ####
filter(data_for_fig,agency%in%c("DOL")) %>%
  ggplot(aes(x=name,y=congress)) +
  geom_point(aes(color=unorthodox))+
  scale_y_continuous(name="Congress",breaks = seq(from=94,to=114,by=2)) +
  coord_flip() +
  theme_minimal() +
  theme(axis.title.y=element_blank(),
        legend.position = "bottom",
        legend.title = element_blank())


#### Figure A7 ####
## Make Dataset
committee_grants <- grants %>%
  filter(!is.na(on_commit) & !is.na(on_commit_prior) & !is.na(grant)) %>%
  mutate(treated = on_commit,
         control = 1 - on_commit,
         state_year = paste0(state_abbr,year),
         state_year_agency = paste0(state_abbr,year,agency),
         state_year_commit = paste0(state_abbr,year,committee_code),
         state_program = paste0(state_abbr, program),
         commit_year = paste(committee_code, year),
         program_year = paste(program, year),
         log_grant = log(grant+1),
         prior_year = year - 1) 
## Create lags and leads and calculate weights  
commit_analysis_data <- make_boot_data(treat_var = "treated",
                                       untreat_var = "control",
                                       time_var = "year", 
                                       lag_control = "control",
                                       compare_var = "state_year_commit", 
                                       unit_var= "state_program", d_var="log_grant", 
                                       lead_yrs = 2,lag_yrs = 2, df = committee_grants)
## Make Figure
commit_analysis_data %>%
  rename(log_grant_t0 = log_grant) %>%
  pivot_longer(cols = starts_with("log_grant"), names_to="time",
               names_prefix="log_grant_",values_to="log_grant") %>%
  mutate(time = case_when(
    time=="p2" ~ -2,
    time=="p1" ~ -1,
    time=="t0" ~ 0,
    time=="t1" ~ 1,
    time=="t2" ~ 2
  )) %>%
  mutate(grant = exp(log_grant)-1,
         obs_weight_fig = abs(obs_weight)) %>%
  filter(obs_weight!=0) %>%
  group_by(treated,time) %>%
  summarise(grant = weighted.mean(x=grant, w=obs_weight_fig, na.rm=T)) %>%
  mutate(pre_post = ifelse(time<0,1,0),
         pre_post = paste0(pre_post,treated)) %>%
  ggplot(aes(x=time,y=grant/1000000,color=factor(treated),
             group=pre_post)) +
  geom_point() +
  geom_line() +
  geom_vline(xintercept = -0.5,lty="dashed")+
  scale_y_continuous(labels = scales::dollar,
                     name="Avg State Grant (in Millions)") +
  scale_x_continuous(labels=c("t-2","t-1", "t", "t+1", "t+2"),
                     name=NULL) +
  scale_color_manual(values=c("grey","black"),
                     labels = c("Not on Committee",
                                "Committee Member"),
                     name = NULL) +
  theme_minimal() +
  theme(legend.position = "bottom")

#### Size of Changes: Unorthodox vs Regular Order (Figure A8) ####
grants_to_plot <- grants %>%
  mutate(prior_year = year - 1) %>%
  left_join(select(grants, prior_year=year,prior_grant = grant,state_program),
            by = c("prior_year","state_program")) %>%
  group_by(program_year) %>%
  mutate(total_funding = sum(grant, na.rm = T),
         prior_total_funding = sum(prior_grant, na.rm = T)) %>%
  ungroup()%>%
  mutate(share = grant / total_funding,
         share_prior = prior_grant / prior_total_funding)

grants_to_plot$log_diff <- log(grants_to_plot$grant+1) - 
  log(grants_to_plot$prior_grant+1)
grants_to_plot$share_diff <- (grants_to_plot$share * 100) - 
  (grants_to_plot$share_prior * 100)

grant_change_log <- lm(log_diff~unorthodox, data=grants_to_plot, subset = year==year_auth_starts & grant!=0) 
grant_change_share <- lm(share_diff~unorthodox, data=grants_to_plot, subset = year==year_auth_starts & grant!=0) 


data.frame(estimate=c(coef(grant_change_log)[2],coef(grant_change_share)[2]),
           confint_lower = c(confint(grant_change_log)[2,1], confint(grant_change_share)[2,1]),
           confint_upper = c(confint(grant_change_log)[2,2], confint(grant_change_share)[2,2]),
           label = c("Log Dollars", "Share")) %>%
  ggplot(aes(x=label,y=estimate)) +
  geom_point() +
  geom_linerange(aes(ymin=confint_lower, ymax=confint_upper)) +
  geom_hline(yintercept=0,lty="dashed") +
  ylab("Difference in Means") +
  theme_minimal()+
  theme(axis.title.x = element_blank(),
        text= element_text(size=13))
